from calendar import c
from feature import FileParse
import os
import csv
import re
import time
import json
def main():
    #Datadir为当前存放数据集的文件夹名称
    Datadir='/home/codedataset/CodePerspective-cpp/resultcode'
    file_error='data_error.txt'
    fr=open(file_error,'w',encoding='utf-8')
    for dir in os.listdir(Datadir):
                #Author为作者名称
        Author = dir
        paths="./data/"+dir+"/"
        if not os.path.exists(paths):
            os.makedirs(paths)
        content = []
        #大五人格信息，现在暂不考虑
        value_info={
        'openness':0,
        'conscientiousness':0,
        'extroversion':0,
        'agreeableness':0,
        'neuroticism':0
        }
        ll=0
        file_list = []
        #一种读取逻辑
        for dirpath, dirname, files in os.walk(Datadir + '/' + dir):
            if dirname!=[]:
                for dirs in dirname:
                #if dirs=='C++':
                    for dirpaths,dirnames,filess in os.walk(Datadir+'/'+dir+'/'+dirs):
                        for fi in filess:
                            if fi.find(".cpp")!=-1 or fi.find(".c")!=-1 or fi.find(".h")!=-1:
                                file_path = os.path.join(dirpath+'/'+dirs, fi)
                                if "\\" in file_path:
                                    file_path = file_path.replace('\\', '/')
                                file_list.append(file_path)
            else:
                for file in files:
                    if file.find(".cpp")!=-1 or file.find(".c")!=-1 or file.find(".h")!=-1:
                        file_path = os.path.join(dirpath, file)
                        if "\\" in file_path:
                            file_path = file_path.replace('\\', '/')
                        file_list.append(file_path)

        #另一种读取逻辑
            #for file in files:
             #   forbidfile=r"csv"
              #  if re.findall(forbidfile,file)!=[]:
               #     continue
                #ll+=1
                #file_path = os.path.join(dirpath, file)
                #if "\\" in file_path:
                 #   file_path = file_path.replace('\\', '/')
                #file_list.append(file_path)
        st_all=time.time()
        author_info=[]
        for file in file_list:
            try:
                parser = FileParse.FileParser()
                print("---"+file+"---")
                author_info_1=parser.parse(file)
                file_info={"PersonName":Author,"PersonPath":file,"FileFeatures":author_info_1}
                author_info.append(file_info)
            except:
                fr = open(file_error, 'w', encoding='utf-8')
                fr.write(file)
                fr.flush()
        dats=json.dumps(author_info,ensure_ascii=False,indent=1)
        with open("./software_project/"+Author+".json",'w',newline='\n',encoding='utf-8') as f:
            f.write(dats)
    fr.close()
    print("finished!!!")
if __name__ == '__main__':
    main()
